/// <reference path="../../index.d.ts" />
"use strict"
/**
 * @param {import('egg').Application} app
 */
function initRelation(app) {
	let ModuleApplicat = app.model.ModuleApplicat
	let Module = app.model.Module
	let Application = app.model.Application
	let ApplicationUser = app.model.ApplicationUser
	let UnionUser = app.model.UnionUser
	let AmoPartci = app.model.AmoPartci
	let UmPartci = app.model.UmPartci
	let AssistData = app.model.Game.AssistData
	let GameUser = app.model.Game.GameUser
	ModuleApplicat.belongsTo(Module, {
		foreignKey: "mid"
	})
	Module.hasMany(ModuleApplicat,
		{
			foreignKey: "mid"
		})
	ModuleApplicat.belongsTo(Application, {
		foreignKey: "aid",
	})


	AmoPartci.belongsTo(ApplicationUser, {foreignKey: "oid"})
	UmPartci.belongsTo(UnionUser, {foreignKey: "uid"})

	AssistData.belongsTo(ApplicationUser, {foreignKey: "helpid"})
	GameUser.belongsTo(ApplicationUser, {foreignKey: "oid"})
	// console.log("关系\n>>加载完成")

	app.model.query("SET @@global.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'", {logging: false})
	//用户统计
	app.model.query(`
	create OR REPLACE view empire_user_view AS 
	SELECT
			application_user.aid,
			application_user.headimg_url,
			empire_user.oid,
			application_user.openid,
			application_user.unionid,
			application_user.nickname,
			parentQuery.nickname as invitedName,
			empire_user.total_coin,
			empire_user.popularity,
			empire_user.sycee,
			empire_user.ip,
			application_user.sex,
			application_user.create_at,
			empire_user.last_login,
			empire_user.inviteid
	FROM
			empire_user
	LEFT JOIN empire_user AS parentUser
	ON
			(
					empire_user.inviteid = parentUser.oid
			)
	LEFT JOIN application_user ON empire_user.oid = application_user.id
	LEFT JOIN application_user as parentQuery ON empire_user.inviteid = parentQuery.id ORDER BY application_user.create_at DESC`, {logging: false})
	//商店统计视图
	app.model.query(`create OR REPLACE view empire_store_count AS 
	SELECT empire_user.oid,COUNT(empire_user_store.oid) as count FROM empire_user
	LEFT JOIN empire_user_store ON empire_user.oid=empire_user_store.oid AND empire_user.sell_count=empire_user_store.sales_round
	GROUP BY empire_user.oid `, {logging: false})
	/* 	*/
	if (process.env.EGG_SERVER_ENV != "prod") {
		// app.model.query("SET @query_cache_type=1")
	}
	// try {
	// upDate(app).catch(console.log)
}
/**
 * @param {import('egg').Application} app
 */
async function upDate(app) {
	let sequelize = app.model
	let ids = await sequelize.query("select  ad_channel_count.id from ad_channel_count , ( SELECT MAX(ad_channel_count.adClick) as adClick ,id,oid,ad_channel_count.date FROM `ad_channel_count`   GROUP BY ad_channel_count.oid,ad_channel_count.date ) as mydata WHERE ad_channel_count.oid=mydata.oid AND ad_channel_count.date=mydata.date and mydata.id <> ad_channel_count.id", {
		type: sequelize.QueryTypes.SELECT
	})
	await sequelize.AdCount.destroy({
		where: {
			id: {
				[sequelize.Op.in]: ids.map(obj => obj.id)
			}
		}
	})
	app.logger.warn(JSON.stringify(ids))
	await sequelize.query("ALTER TABLE `ad_channel_count` ADD UNIQUE( `oid`, `date`);")
}
module.exports = initRelation